package com.icoolkj.mall.user.controller;

import com.icoolkj.mall.common.Result;
import com.icoolkj.mall.user.dto.OrderResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/01/20 15:51
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController implements InitializingBean{

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/getOrderByUserId")
    public Result<List<OrderResponse>> getOrderByUserId(@RequestParam("userId") Long userId) {
        log.info("根据userId：{} 查询订单信息", userId);
        // 方式1：restTemplate 调用，url 写死
//        String url = "http://localhost:8560/api/order/getOrderByUserId?userId=" + userId;

        //http://localhost:8560/api/order/getOrderByUserId?userId=1
        String url = "http://icoolkj-mall-order01/api/order/getOrderByUserId?userId=" + userId;

        Result<List<OrderResponse>> result = restTemplate.getForObject(url, Result.class);

        return result;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
//        String url = "http://icoolkj-mall-order01/api/order/getOrderByUserId?userId=1";
//        Result<List<OrderResponse>> result = restTemplate.getForObject(url, Result.class);
//        log.info("result =>> {}", result.getData().stream().findFirst().toString());

    }
}
